package com.yuju.controller.login;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.login.enums.MenuReturnCodeEnum;
import com.yuju.login.model.MenuModel;
import com.yuju.login.service.IMenuService;
import com.yuju.model.TreeModel;
import com.yuju.util.Utils;
import com.yuju.vo.login.MenuVO;

/**
 * 
 * 类名：MenuController  <br />
 *
 * 功能：菜单相关
 *
 * @author : zhousc <br />
 * @Date : 2017年12月4日 上午10:55:26  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("menu")
public class MenuController extends BaseController<MenuVO, MenuModel> {

	@Autowired
	private IMenuService menuService;
	
	/**
	 * 
	 * 功能：<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 上午10:56:24 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj) {
		MenuVO  menuVO = obj.toJavaObject(MenuVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(menuVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageModel = menuService.pageList(convert(menuVO), menuVO.getPage(), menuVO.getPageSize());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(MenuReturnCodeEnum.MENU_QUERY_SUCCESS.getCode());
			result.setReturnMsg(MenuReturnCodeEnum.MENU_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 上午10:56:19 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj) {
		MenuVO  menuVO = obj.toJavaObject(MenuVO.class);
		Result result = new Result();
		int rows = menuService.save(convert(menuVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(MenuReturnCodeEnum.MENU_SAVE_SUCCESS.getCode());
			result.setReturnMsg(MenuReturnCodeEnum.MENU_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(MenuReturnCodeEnum.MENU_SAVE_FAILURE.getCode());
			result.setReturnMsg(MenuReturnCodeEnum.MENU_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 上午10:56:16 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj) {
		MenuVO  menuVO = obj.toJavaObject(MenuVO.class);
		Result result = new Result();
		int rows = menuService.updateSelective(convert(menuVO));
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(MenuReturnCodeEnum.MENU_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(MenuReturnCodeEnum.MENU_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(MenuReturnCodeEnum.MENU_UPDATE_FAILURE.getCode());
			result.setReturnMsg(MenuReturnCodeEnum.MENU_UPDATE_FAILURE.getDesc());
		}
		return result;
	}

	/**
	 * 
	 * 功能：<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 上午10:56:10 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj) {
		MenuVO  menuVO = obj.toJavaObject(MenuVO.class);
		Result result = new Result();
		int rows = menuService.del(menuVO.getId().toString());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(MenuReturnCodeEnum.MENU_DEL_SUCCESS.getCode());
			result.setReturnMsg(MenuReturnCodeEnum.MENU_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(MenuReturnCodeEnum.MENU_DEL_FAILURE.getCode());
			result.setReturnMsg(MenuReturnCodeEnum.MENU_DEL_FAILURE.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 上午10:56:13 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj) {
		MenuVO  menuVO = obj.toJavaObject(MenuVO.class);
		Result result = new Result();
		MenuModel MenuModel = menuService.findById(menuVO.getId().toString());
		if (Utils.isNotNull(MenuModel)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(MenuModel);
			result.setReturnCode(MenuReturnCodeEnum.MENU_QUERY_SUCCESS.getCode());
			result.setReturnMsg(MenuReturnCodeEnum.MENU_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：角色编辑页的菜单树<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月4日 下午2:05:52 <br/>
	 */
	@RequestMapping("getMenuTree")
	@ResponseBody
	public Result getMenuTree(@RequestBody JSONObject obj) {
		MenuVO  menuVO = obj.toJavaObject(MenuVO.class);
		Result result = new Result();
		List<TreeModel> list = menuService.getMenuTree(convert(menuVO));
		if (Utils.isNotNull(list)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(list);
			result.setReturnCode(MenuReturnCodeEnum.MENU_QUERY_SUCCESS.getCode());
			result.setReturnMsg(MenuReturnCodeEnum.MENU_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：获取账号所属菜单<br/>
	 *
	 * @author xuehj
	 * @param obj 参数集
	 * @Date 2018年1月17日 上午11:23:55 <br/>
	 */
	public Result getMenuByOp(@RequestBody JSONObject obj) {
		//账号菜单权限范围数据
		List<MenuModel> menuList = menuService.menuListByOpId(obj.getString("opId"));
		
		Result result = new Result();
		result.setStatus(ReturnStatusEnum.FAILURE.getCode());
		result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
		result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
		result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		
		if (Utils.isNull(menuList) || menuList.isEmpty()) {
			return result;
		}
		
		//重新包装菜单
		List<TreeModel> menuTree = menuService.menuTree(menuList);
		
		if (Utils.isNull(menuTree) || menuTree.isEmpty()) {
			return result;
		}
		
		result.setRows(menuTree);
		result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
		result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
		result.setReturnCode(MenuReturnCodeEnum.MENU_QUERY_SUCCESS.getCode());
		result.setReturnMsg(MenuReturnCodeEnum.MENU_QUERY_SUCCESS.getDesc());
		
		return result;
	}
}
